﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Enterprise.Management.Gridview
{
    public class GridviewDataProvider
    {
        public object Get(object obj, string name)
        {
            return GetObjectValue(obj, name);
        }

        private object GetObjectValue(object obj, string name)
        {
            string[] names = name.Split('.');

            var type = obj.GetType();
            var property = type.GetProperty(names[0]);
            if (property != null)
            {
                var myObj = property.GetValue(obj, null);
                if (names.Length > 1)
                { 
                    string subName = string.Empty;
                    for (int i = 1; i < names.Length; i++)
                    {
                        if (subName.Length > 0)
                        {
                            subName += ".";
                        }
                        subName += names[i];
                    }
                    return GetObjectValue(myObj, subName);
                } else {
                    return myObj;
                }
            }
            else
            {
                return null;
            }
        }
    }
}